package vn.sunnet.game.SinhTon.screen;

import java.util.ArrayList;

import vn.sunnet.game.SinhTon.global.Assets;
import vn.sunnet.game.SinhTon.global.Setting;
import vn.sunnet.game.SinhTon.object.BoundObject;
import vn.sunnet.game.SinhTon.scroll.ButtonLevel;
import vn.sunnet.game.SinhTon.scroll.ScrollLayer;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class SelectMap extends BaseScreen {
	private Button btnBack, btnTiepTuc;

	private ArrayList<BoundObject> arrBoundObject;

	private int optionMap = 1;
	private boolean isInvisible;
	// ScrollPanel
	private ScrollLayer scroller;
	private Group group;
	private int mapCurrent;

	// Dung de ve ra cac hinh vuong bao doi tuong(dung de test)
	// private ShapeRenderer renderer;
	public SelectMap(final MyGame myGame) {
		super(myGame);
		game.request.setVisibleAdmod(false);
		if (!Assets.GET_SELECTMAP)
			Assets.getSelectMap();

		initButton();
		group = new Group();
		arrBoundObject = new ArrayList<BoundObject>();
		for (int i = 0; i < 10; i++) {
			arrBoundObject.add(new BoundObject(i + 1));
		}

		Assets.mMainMenu.pause();

		if (Setting.isSound(Setting.MUSIC))
			Assets.mOptionShop.play();
		else
			Assets.mOptionShop.pause();

		for (int i = 0; i < 10; i++) {
			if (Setting.getBoolean("MAP" + (i + 1)))
				mapCurrent = i + 1;
		}

		Assets.map = mapCurrent;
		// renderer = new ShapeRenderer();
	}

	@Override
	public void render(float delta) {
		super.render(delta);
		Gdx.gl.glClearColor(0.5f, 0.6f, 0.5f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		camera.update();
		stage.act(delta);

		batch.begin();

		batch.draw(Assets.ttBgSelectMap, 237, 0, 563, Assets.SCREEN_HEIGHT);

		batch.end();
		stage.draw();
		batch.begin();
		batch.draw(Assets.ttBgMapVN, -1, 0, 237, Assets.SCREEN_HEIGHT);
		batch.draw(Assets.ttPoint, 0, 0, 237, Assets.SCREEN_HEIGHT);

		for (BoundObject bo : arrBoundObject) {
			if (optionMap == bo.getId()) {
				batch.draw(bo.getTexture(), 425, 426, 186, 39);
				batch.draw(Assets.ttPoint1, bo.getBounds().x - 6,
						bo.getBounds().y - 6, bo.getBounds().width + 12,
						bo.getBounds().height + 12);
				break;
			}
		}

		if (isInvisible) {
			batch.setColor(0.7f, 0.7f, 0.7f, 0.7f);
			batch.draw(Assets.trTiepTuc, 622, 7, 173, 56);
			batch.setColor(Color.WHITE);
		}

		batch.end();
		update(delta);
		// renderDebug();
	}

	// private void renderDebug() {
	// renderer.setProjectionMatrix(camera.combined);
	// renderer.begin(ShapeType.Rectangle);
	//
	// for (BoundObject bo : arrBoundObject) {
	// renderer.rect(bo.getBounds().x, bo.getBounds().y,
	// bo.getBounds().width, bo.getBounds().height);
	// }
	//
	// renderer.end();
	// }

	private void update(float time) {
		optionMap = scroller.getCurrentSection();
		// Gdx.app.log(getClass().toString(), Assets.optionMap +
		// " Assets.optionMap");
		// if (optionMap > Assets.map) {
		if (optionMap > mapCurrent) {
			isInvisible = true;
			btnTiepTuc.remove();
		} else {
			isInvisible = false;
			stage.addActor(btnTiepTuc);
		}
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		super.resize(width, height);
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		addScrollLayer();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		super.resume();
	}

	@Override
	public void dispose() {
		super.dispose();
		arrBoundObject.clear();
		if (Assets.mOptionShop != null)
			Assets.mOptionShop.dispose();
		if (Assets.mMainMenu != null)
			Assets.mMainMenu.dispose();
		group.clear();
	}

	@Override
	public boolean keyDown(int keycode) {
		if (keycode == Keys.BACK)
			// game.setScreen(game.menuScreen);
			game.setScreen(new MenuScreen(game));
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		// return super.touchUp(screenX, screenY, pointer, button);
		return false;
	}

	private void initButton() {
		// Nut quay lai
		btnBack = new Button(Assets.skin, "btnBack");
		btnBack.setBounds(242, 9, 77, 49);
		btnBack.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if(Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				// game.setScreen(game.menuScreen);
				game.setScreen(new MenuScreen(game));
			}
		});
		stage.addActor(btnBack);

		// Nut tiep theo
		btnTiepTuc = new Button(Assets.skin, "btnTiepTuc");
		btnTiepTuc.setBounds(622, 6, 173, 56);
		btnTiepTuc.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if(Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				Assets.map = optionMap;
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				
				game.request.trackStartLevel(Assets.map);
				game.stageScreen = new StageScreen(game);
				game.setScreen(game.stageScreen);
			}
		});
		// stage.addActor(btnTiepTuc);
	}

	private void addScrollLayer() {
		scroller = new ScrollLayer(group, 563, Assets.SCREEN_HEIGHT,
				Assets.SCREEN_WIDTH);

		stage.addActor(scroller);

		ScrollLayer.onMoveListener listener = new ScrollLayer.onMoveListener() {
			@Override
			public void StartMove() {
				// do something when the page start moving....
			}

			@Override
			public void EndMove(int CurrentPage, int PageCount) {
				// do something when the page end moving....
			}
		};
		scroller.setListener(listener);

		addPages();
	}

	private void addPages() {
		ButtonLevel.onClickListener listener = new ButtonLevel.onClickListener() {
			@Override
			public void onUp(int id) {
				// do something when click on one of the button level
			}
		};

		ButtonLevel button;
		// add 10 pages
		for (int i = 0; i < 10; i++) {
			Table table = new Table();
			button = new ButtonLevel(
					i + 1,
					Setting.getBoolean("MAP" + (i + 1)) == true ? Assets.trMapOption
							: Assets.trMapOptionLock, listener);
			// Canh left = 440, top = 10 hoac tuy chinh trong class ScrollLayer
			// cho
			// group.setX de duoc vi tri phu hop
			table.add(button).pad(10, 440, 0, 0); // canh top 10 va left 440
			scroller.addWidget(table);
		}
	}

}
